<?php 
class ApiCenter_ProductApi_Helper_Categorytree extends Mage_Core_Helper_Data
{
	
        private static $_allData = array();
		
        
        public function getCategoryTree($isHtml = true,$storeid = 0){
        $html ="";
        $parent_id = 2;
        $collection = Mage::getModel('catalog/category')->getCollection();
        
        $collection->addFieldToFilter('parent_id',array('gteq' => array($parent_id)));
        $collection->getSelect()
        ->joinLeft(array('f' => 'catalog_category_entity_varchar'),
            'e.entity_id = f.entity_id',
            array(
                "text"=>"f.value",
            ))
		 ->joinLeft(array('g' => 'catalog_category_entity_url_key'),
            'e.entity_id = g.entity_id',
            array(
                "urlkey"=>"g.value",
            ))	
            ->joinLeft(array("k"=>"rumall_attribute_bind_category"),
                    'e.entity_id = k.category_id',
                    array(
                        "set_id"=>"k.attributeset_id"
                    )
                )
            ->where("f.attribute_ID = 41 AND f.store_id = ".$storeid." AND g.store_id = ".$storeid);
        $nodes = $collection->getData();
		$baseurl =  Mage::getBaseUrl();
		self::$_allData = $nodes;
        $topNodes = self::getTopNodes($nodes,$baseurl);
        foreach ($topNodes as $node) {
            $tree[] = self::getSubNodes($node,$baseurl,$node['urlkey']."/");
        }
        if($isHtml){
            $html.='<ul class="row topListMenu">';
            $html.=self::displayTree($tree);
            $html.="</ul>";
            return  $html;
        }else{
            return $tree;
        }	
    }

     public static function displayTree($tree,$leven =0,$parentkey="")
     {
         $str ="";
         $parentKey ="";
         $outermostClassCode = "";
         if ($leven == 0 ) {
             $outermostClassCode = 'level-top em-menu-link ';
         }

         $navRule ="";
        foreach ($tree as $key => $node) {
            $parentKey = $parentkey.($key+1).'-';
            $navRule = self::liClass($node, $key, $tree);
             $str .= '<li class="level'.$leven.' '.'nav-'.rtrim($parentKey, "-").' '.$outermostClassCode.$navRule.'">';
              $str.= '<a href = "'.$node['url'].'" class="'.$outermostClassCode.'" target="_blank"><span>';
             $str.= self::escapeHtml($node['name']);
             $str.="</span></a>";
             
				if(!$node['is_leaf']){
				$str .= '<ul class="level'.(10+$leven).'">';
                $str .=  self::displayTree($node['children'], $leven+1,$parentKey);
                $str .= '</ul>';
				}
			$str .='</li>';
       }
	   
	   return $str;
    }
    private  static function liClass($node,$key,$tree){
        $str = "";
        $num = count($tree)-1;
        if($key == 0){
            $str.= "first ";
        }else if($num == $key){
            $str.= "last ";
        }
        if(!$node['is_leaf']){
            $str.="parent ";
        }
     return $str;
    }
    private static function getTopNodes($nodes,$baseurl)
    {
        $data = array();

        foreach ($nodes as $row)
        {
            if ($row['parent_id'] == '2')
            {
                $node = array();
                $node['id'] = $row['entity_id'];
                $node['name'] = $row['text'];
				$node['url'] = $baseurl.$row['urlkey'];
				$node['urlkey'] = $row['urlkey'];
                $node['is_leaf'] = true;
                $node['children'] = array();
                if( !empty($row['set_id']) ){
                    $node['set_id'] = $row['set_id'];
                }else{
                    $node['set_id'] = 0;
                }
                $data[] = $node;
            }
        }
        return $data;
    }
    private static function getSubNodes(array $node,$baseurl="",$urlkey="")
    {
        foreach (self::$_allData as $key => $row)
        {
            if ($row['parent_id'] == $node['id'])
            {
                $matchurl = $urlkey.$row['urlkey']."/";
                $subNode = array();
                $subNode['id'] = $row['entity_id'];
                $subNode['name'] = $row['text'];
				$subNode['url'] = $baseurl.$matchurl;
                $subNode['is_leaf'] = true;
                if(!empty($row['set_id'])){
                    $subNode['set_id'] = $row['set_id'];
                }else{
                    $subNode['set_id'] = 0;
                }
                $subNode['children'] = array();
                $node['children'][] = self::getSubNodes($subNode,$baseurl,$matchurl);
            }
        }
        if (count($node['children']) > 0) {
            $node['is_leaf'] = false;
        }
        return $node;
    }
}
?>